package com.guowen.myaccount.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;

import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.guowen.myaccount.AccManagementActivity;
import com.guowen.myaccount.HistoryActivity;
import com.guowen.myaccount.MainActivity;
import com.guowen.myaccount.PersonActivity;
import com.guowen.myaccount.R;
import com.guowen.myaccount.RecommendActivity;
import com.guowen.myaccount.SortActivity;
import com.guowen.myaccount.StatisticsActivity;
import com.guowen.myaccount.adapter.MenuAdapter;
import com.guowen.myaccount.db.MenuBean;

import java.util.List;

public class MenuDialog extends Dialog {

    private ListView menuLv1, menuLv2;
    private List<MenuBean> mDatas1, mDatas2;
    private MenuAdapter adapter1, adapter2;
    private LinearLayout linearLayout;

    public MenuDialog(@NonNull Context context,List<MenuBean> mDatas1, List<MenuBean> mDatas2) {
        super(context);
        this.mDatas1 = mDatas1;
        this.mDatas2 = mDatas2;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_menu);

        linearLayout = findViewById(R.id.dialog_menu_ll);

        setLlListener();

        //加载一行行的目录
        menuLv1 = findViewById(R.id.main_menu_lv1);
        adapter1 = new MenuAdapter(getContext(),mDatas1);
        menuLv1.setAdapter(adapter1);
        //加载更多下面的目录
        menuLv2 = findViewById(R.id.main_menu_lv2);
        adapter2 = new MenuAdapter(getContext(),mDatas2);
        menuLv2.setAdapter(adapter2);

        //给菜单一的ListView设置监听
        addLV1Listener();
        //给菜单二的ListView设置监听
        addLV2Listener();

    }

    private void setLlListener() {
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PersonActivity.class);
                getContext().startActivity(intent);
            }
        });
        linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                PasswordDialog dialog = new PasswordDialog(getContext());
                dialog.show();
                return true;
            }
        });
    }

    private void addLV2Listener() {
        menuLv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                switch (position) {
                    case 0:
                        intent = new Intent(getContext(), RecommendActivity.class);
                        getContext().startActivity(intent);
                        break;
                    case 4:
                        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                        builder.setTitle("确定退出用户界面嘛？").setPositiveButton("确定", new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 设置转变为匿名界面刷新
                                MainActivity.userId = 0;
                                Intent intent = new Intent(getContext(),MainActivity.class);
                                getContext().startActivity(intent);
                            }
                        }).setNegativeButton("取消", null).show();
                        break;
                    default:
                        Toast.makeText(getContext(),"此功能还未开发，敬请期待！",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    private void addLV1Listener() {
        menuLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                switch (position) {
                    case 0:
                        if (getContext().getClass() == MainActivity.class)
                            break;
                        adapter1.selectPos = 0;
                        adapter1.notifyDataSetChanged();
                        intent = new Intent(getContext(), MainActivity.class);
                        getContext().startActivity(intent);
                        MenuDialog.this.cancel();
                        break;
                    case 1:
                        intent = new Intent(getContext(), HistoryActivity.class);
                        getContext().startActivity(intent);
                        MenuDialog.this.cancel();
                        break;
                    case 2:
                        intent = new Intent(getContext(), StatisticsActivity.class);
                        getContext().startActivity(intent);
                        MenuDialog.this.cancel();
                        break;
                    case 3:
                        intent = new Intent(getContext(), SortActivity.class);
                        getContext().startActivity(intent);
                        MenuDialog.this.cancel();
                        break;
                    case 4:
                        intent = new Intent(getContext(), AccManagementActivity.class);
                        getContext().startActivity(intent);
                        MenuDialog.this.cancel();
                        break;
                    case 5:
                        if (getContext().getClass() == PersonActivity.class){
                            break;
                        }
                        adapter1.selectPos = 5;
                        adapter1.notifyDataSetChanged();
                        intent = new Intent(getContext(), PersonActivity.class);
                        getContext().startActivity(intent);
                        MenuDialog.this.cancel();
                        break;
                }
            }
        });
    }


    // 设置Dialog的尺寸和屏幕尺寸一致
    public void setDialogSize(){
        //获取当前窗口对象
        Window window = getWindow();
        //获取窗口对象的参数
        WindowManager.LayoutParams wlp = window.getAttributes();
        //获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.height = (int)(d.getHeight()); // 对话框窗口为屏幕窗口
        wlp.gravity = Gravity.LEFT;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);
    }
}
